home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / SphereMotion / SphereMotion.java.z / SphereMotion.java
Encoding:
Java Source  |  2003-08-08  |  9.6 KB  |  297 lines

  1. /*
  2.  *    @(#)SphereMotion.java 1.38 02/10/21 13:55:06
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.*;
  42. import com.sun.j3d.utils.applet.MainFrame;
  43. import com.sun.j3d.utils.geometry.Sphere;
  44. import com.sun.j3d.utils.universe.*;
  45. import javax.media.j3d.*;
  46. import javax.vecmath.*;
  47. import java.util.Enumeration;
  48.  
  49. public class SphereMotion extends Applet {
  50.  
  51.     // Constants for type of light to use
  52.     private static final int DIRECTIONAL_LIGHT = 0;
  53.     private static final int POINT_LIGHT = 1;
  54.     private static final int SPOT_LIGHT = 2;
  55.  
  56.     // Flag indicates type of lights: directional, point, or spot
  57.     // lights.  This flag is set based on command line argument
  58.     private static int lightType = POINT_LIGHT;
  59.  
  60.     private SimpleUniverse u = null;
  61.  
  62.     public BranchGroup createSceneGraph(SimpleUniverse u) {
  63.     Color3f eColor    = new Color3f(0.0f, 0.0f, 0.0f);
  64.     Color3f sColor    = new Color3f(1.0f, 1.0f, 1.0f);
  65.     Color3f objColor  = new Color3f(0.6f, 0.6f, 0.6f);
  66.     Color3f lColor1   = new Color3f(1.0f, 0.0f, 0.0f);
  67.     Color3f lColor2   = new Color3f(0.0f, 1.0f, 0.0f);
  68.     Color3f alColor   = new Color3f(0.2f, 0.2f, 0.2f);
  69.     Color3f bgColor   = new Color3f(0.05f, 0.05f, 0.2f);
  70.  
  71.     Transform3D t;
  72.  
  73.     // Create the root of the branch graph
  74.     BranchGroup objRoot = new BranchGroup();
  75.  
  76.         // Create a Transformgroup to scale all objects so they
  77.         // appear in the scene.
  78.         TransformGroup objScale = new TransformGroup();
  79.         Transform3D t3d = new Transform3D();
  80.         t3d.setScale(0.4);
  81.         objScale.setTransform(t3d);
  82.         objRoot.addChild(objScale);
  83.  
  84.     // Create a bounds for the background and lights
  85.     BoundingSphere bounds =
  86.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  87.  
  88.     // Set up the background
  89.     Background bg = new Background(bgColor);
  90.     bg.setApplicationBounds(bounds);
  91.     objScale.addChild(bg);
  92.  
  93.     // Create a Sphere object, generate one copy of the sphere,
  94.     // and add it into the scene graph.
  95.     Material m = new Material(objColor, eColor, objColor, sColor, 100.0f);
  96.     Appearance a = new Appearance();
  97.     m.setLightingEnable(true);
  98.     a.setMaterial(m);
  99.     Sphere sph = new Sphere(1.0f, Sphere.GENERATE_NORMALS, 80, a);
  100.     objScale.addChild(sph);
  101.  
  102.     // Create the transform group node for the each light and initialize
  103.     // it to the identity.  Enable the TRANSFORM_WRITE capability so that
  104.     // our behavior code can modify it at runtime.  Add them to the root
  105.     // of the subgraph.
  106.     TransformGroup l1RotTrans = new TransformGroup();
  107.     l1RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  108.     objScale.addChild(l1RotTrans);
  109.  
  110.     TransformGroup l2RotTrans = new TransformGroup();
  111.     l2RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  112.     objScale.addChild(l2RotTrans);
  113.  
  114.     // Create transformations for the positional lights
  115.     t = new Transform3D();
  116.     Vector3d lPos1 =  new Vector3d(0.0, 0.0, 2.0);
  117.     t.set(lPos1);
  118.     TransformGroup l1Trans = new TransformGroup(t);
  119.     l1RotTrans.addChild(l1Trans);
  120.  
  121.     t = new Transform3D();
  122.     Vector3d lPos2 = new Vector3d(0.5, 0.8, 2.0);
  123.     t.set(lPos2);
  124.     TransformGroup l2Trans = new TransformGroup(t);
  125.     l2RotTrans.addChild(l2Trans);
  126.  
  127.     // Create Geometry for point lights
  128.     ColoringAttributes caL1 = new ColoringAttributes();
  129.     ColoringAttributes caL2 = new ColoringAttributes();
  130.     caL1.setColor(lColor1);
  131.     caL2.setColor(lColor2);
  132.     Appearance appL1 = new Appearance();
  133.     Appearance appL2 = new Appearance();
  134.     appL1.setColoringAttributes(caL1);
  135.     appL2.setColoringAttributes(caL2);
  136.     l1Trans.addChild(new Sphere(0.05f, appL1));
  137.     l2Trans.addChild(new Sphere(0.05f, appL2));
  138.  
  139.     // Create lights
  140.     AmbientLight aLgt = new AmbientLight(alColor);
  141.  
  142.     Light lgt1 = null;
  143.     Light lgt2 = null;
  144.  
  145.     Point3f lPoint  = new Point3f(0.0f, 0.0f, 0.0f);
  146.     Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
  147.     Vector3f lDirect1 = new Vector3f(lPos1);
  148.     Vector3f lDirect2 = new Vector3f(lPos2);
  149.     lDirect1.negate();
  150.     lDirect2.negate();
  151.  
  152.     switch (lightType) {
  153.     case DIRECTIONAL_LIGHT:
  154.         lgt1 = new DirectionalLight(lColor1, lDirect1);
  155.         lgt2 = new DirectionalLight(lColor2, lDirect2);
  156.         break;
  157.     case POINT_LIGHT:
  158.         lgt1 = new PointLight(lColor1, lPoint, atten);
  159.         lgt2 = new PointLight(lColor2, lPoint, atten);
  160.         break;
  161.     case SPOT_LIGHT:
  162.         lgt1 = new SpotLight(lColor1, lPoint, atten, lDirect1,
  163.                  25.0f * (float)Math.PI / 180.0f, 10.0f);
  164.         lgt2 = new SpotLight(lColor2, lPoint, atten, lDirect2,
  165.                  25.0f * (float)Math.PI / 180.0f, 10.0f);
  166.         break;
  167.     }
  168.  
  169.     // Set the influencing bounds
  170.     aLgt.setInfluencingBounds(bounds);
  171.     lgt1.setInfluencingBounds(bounds);
  172.     lgt2.setInfluencingBounds(bounds);
  173.  
  174.     // Add the lights into the scene graph
  175.     objScale.addChild(aLgt);
  176.     l1Trans.addChild(lgt1);
  177.     l2Trans.addChild(lgt2);
  178.  
  179.     // Create a new Behavior object that will perform the desired
  180.     // operation on the specified transform object and add it into the
  181.     // scene graph.
  182.     Transform3D yAxis = new Transform3D();
  183.     Alpha rotor1Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
  184.                      0, 0,
  185.                      4000, 0, 0,
  186.                      0, 0, 0);
  187.     RotationInterpolator rotator1 =
  188.         new RotationInterpolator(rotor1Alpha,
  189.                      l1RotTrans,
  190.                      yAxis,
  191.                      0.0f, (float) Math.PI*2.0f);
  192.     rotator1.setSchedulingBounds(bounds);
  193.     l1RotTrans.addChild(rotator1);
  194.  
  195.     // Create a new Behavior object that will perform the desired
  196.     // operation on the specified transform object and add it into the
  197.     // scene graph.
  198.     Alpha rotor2Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
  199.                      0, 0,
  200.                      1000, 0, 0,
  201.                      0, 0, 0);
  202.     RotationInterpolator rotator2 =
  203.         new RotationInterpolator(rotor2Alpha,
  204.                      l2RotTrans,
  205.                      yAxis,
  206.                      0.0f, 0.0f);
  207.     bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  208.     rotator2.setSchedulingBounds(bounds);
  209.     l2RotTrans.addChild(rotator2);
  210.  
  211.     // Create a position interpolator and attach it to the view
  212.     // platform
  213.     TransformGroup vpTrans =
  214.         u.getViewingPlatform().getViewPlatformTransform();
  215.     Transform3D axisOfTranslation = new Transform3D();
  216.     Alpha transAlpha = new Alpha(-1,
  217.                       Alpha.INCREASING_ENABLE |
  218.                       Alpha.DECREASING_ENABLE,
  219.                       0, 0,
  220.                       5000, 0, 0,
  221.                       5000, 0, 0);
  222.     axisOfTranslation.rotY(-Math.PI/2.0);
  223.     PositionInterpolator translator =
  224.         new PositionInterpolator(transAlpha,
  225.                      vpTrans,
  226.                      axisOfTranslation,
  227.                      2.0f, 3.5f);
  228.     translator.setSchedulingBounds(bounds);
  229.     objScale.addChild(translator);
  230.  
  231.         // Let Java 3D perform optimizations on this scene graph.
  232.         objRoot.compile();
  233.  
  234.     return objRoot;
  235.     }
  236.  
  237.     public SphereMotion() {
  238.     }
  239.  
  240.     public void init() {
  241.     setLayout(new BorderLayout());
  242.         GraphicsConfiguration config =
  243.            SimpleUniverse.getPreferredConfiguration();
  244.  
  245.         Canvas3D c = new Canvas3D(config);
  246.     add("Center", c);
  247.  
  248.     u = new SimpleUniverse(c);
  249.     BranchGroup scene = createSceneGraph(u);
  250.  
  251.         // This will move the ViewPlatform back a bit so the
  252.         // objects in the scene can be viewed.
  253.         u.getViewingPlatform().setNominalViewingTransform();
  254.  
  255.     u.addBranchGraph(scene);
  256.     }
  257.  
  258.     public void destroy() {
  259.     u.cleanup();
  260.     }
  261.  
  262.     //
  263.     // The following allows SphereMotion to be run as an application
  264.     // as well as an applet
  265.     //
  266.     public static void main(String[] args) {
  267.         // Parse the Input Arguments
  268.     String usage = "Usage: java SphereMotion [-point | -spot | -dir]";
  269.         for (int i = 0; i < args.length; i++) {
  270.             if (args[i].startsWith("-")) {
  271.                 if (args[i].equals("-point")) {
  272.             System.out.println("Using point lights");
  273.                     lightType = POINT_LIGHT;
  274.                 }
  275.         else if (args[i].equals("-spot")) {
  276.             System.out.println("Using spot lights");
  277.                     lightType = SPOT_LIGHT;
  278.                 }
  279.         else if (args[i].equals("-dir")) {
  280.             System.out.println("Using directional lights");
  281.                     lightType = DIRECTIONAL_LIGHT;
  282.                 }
  283.         else {
  284.             System.out.println(usage);
  285.                     System.exit(0);
  286.                 }
  287.             }
  288.         else {
  289.         System.out.println(usage);
  290.         System.exit(0);
  291.         }
  292.         }
  293.  
  294.     new MainFrame(new SphereMotion(), 700, 700);
  295.     }
  296. }
  297.